package com.lzb.play.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.play.BaseAc;
import com.lzb.play.HomeActivity;
import com.lzb.play.R;
import com.lzb.play.adapter.SetGridAdapter;
import com.lzb.play.bean.GridBean;
import com.lzb.play.bean.PianoKeyBean;
import com.lzb.play.databinding.ActivityGridSettingBinding;
import com.lzb.play.dialog.SelBirdDialog;
import com.lzb.play.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

public class GridSettingActivity extends BaseAc<ActivityGridSettingBinding> implements View.OnClickListener, OnItemClickListener {

    private List<GridBean> listShow = new ArrayList<>();
    private SetGridAdapter setGridAdapter;
    private SelBirdDialog selBirdDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_grid_setting;
    }

    @Override
    protected void initView() {
        mDataBinding.ivGridSettingBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivSaveGrid.setOnClickListener(this);

        mDataBinding.rvSetGridList.setLayoutManager(new GridLayoutManager(mContext, 5));
        setGridAdapter = new SetGridAdapter();
        mDataBinding.rvSetGridList.setAdapter(setGridAdapter);
        setGridAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivSaveGrid) {//保存
            AppSpUtils.saveGridList(listShow);
            ToastUtils.showShort(getString(R.string.save_suc));
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        showSelBirdDialog(position);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        List<GridBean> listSp = AppSpUtils.getGridList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
        } else {
            listSp = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                listSp.add(new GridBean(0, false, false));
            }
            AppSpUtils.saveGridList(listSp);
            listShow.addAll(listSp);
        }
        setGridAdapter.setList(listShow);
    }

    private void showSelBirdDialog(int position) {
        if (selBirdDialog == null) selBirdDialog = new SelBirdDialog(mContext);
        selBirdDialog.setListener(new SelBirdDialog.Ilistener() {
            @Override
            public void clickConfirm(Integer id) {
                listShow.get(position).setRawId(id);
                listShow.get(position).setSetting(true);
                setGridAdapter.setList(listShow);
            }
        });
        selBirdDialog.show();
    }

}